package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.WarehouseOrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.DispatchOrderInfo;
import com.ruoyi.system.mapper.DispatchOrderMapper;
import com.ruoyi.system.domain.DispatchOrder;
import com.ruoyi.system.service.IDispatchOrderService;

/**
 * 出库单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-10
 */
@Service
public class DispatchOrderServiceImpl implements IDispatchOrderService 
{
    @Autowired
    private DispatchOrderMapper dispatchOrderMapper;

    /**
     * 查询出库单
     * 
     * @param id 出库单主键
     * @return 出库单
     */
    @Override
    public DispatchOrder selectDispatchOrderById(Long id)
    {
        return dispatchOrderMapper.selectDispatchOrderById(id);
    }

    /**
     * 查询出库单列表
     * 
     * @param dispatchOrder 出库单
     * @return 出库单
     */
    @Override
    public List<DispatchOrder> selectDispatchOrderList(DispatchOrder dispatchOrder)
    {
        return dispatchOrderMapper.selectDispatchOrderList(dispatchOrder);
    }

    /**
     * 新增出库单
     * 
     * @param dispatchOrder 出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDispatchOrder(DispatchOrder dispatchOrder)
    {
        String userName = SecurityContextHolder.getUserName();
        dispatchOrder.setCreateOrderUser(userName);
        dispatchOrder.setCreateTime(DateUtils.getNowDate());
        int rows = dispatchOrderMapper.insertDispatchOrder(dispatchOrder);
        insertDispatchOrderInfo(dispatchOrder);
        return rows;
    }

    /**
     * 修改出库单
     * 
     * @param dispatchOrder 出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDispatchOrder(DispatchOrder dispatchOrder)
    {
        dispatchOrderMapper.deleteDispatchOrderInfoByDispatchOrderId(dispatchOrder.getId());
        insertDispatchOrderInfo(dispatchOrder);
        return dispatchOrderMapper.updateDispatchOrder(dispatchOrder);
    }

    /**
     * 批量删除出库单
     * 
     * @param ids 需要删除的出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDispatchOrderByIds(Long[] ids)
    {
        dispatchOrderMapper.deleteDispatchOrderInfoByDispatchOrderIds(ids);
        return dispatchOrderMapper.deleteDispatchOrderByIds(ids);
    }

    /**
     * 删除出库单信息
     * 
     * @param id 出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDispatchOrderById(Long id)
    {
        dispatchOrderMapper.deleteDispatchOrderInfoByDispatchOrderId(id);
        return dispatchOrderMapper.deleteDispatchOrderById(id);
    }

    /**
     * 新增出库订单详情信息
     * 
     * @param dispatchOrder 出库单对象
     */
    public void insertDispatchOrderInfo(DispatchOrder dispatchOrder)
    {
        List<DispatchOrderInfo> dispatchOrderInfoList = dispatchOrder.getDispatchOrderInfoList();
        Long id = dispatchOrder.getId();
        if (StringUtils.isNotNull(dispatchOrderInfoList))
        {
            List<DispatchOrderInfo> list = new ArrayList<DispatchOrderInfo>();
            List<WarehouseOrderInfo> warehouseOrderInfoList = new ArrayList<WarehouseOrderInfo>();
            for (DispatchOrderInfo dispatchOrderInfo : dispatchOrderInfoList)
            {

                dispatchOrderInfo.setDispatchOrderId(id);
                list.add(dispatchOrderInfo);

                WarehouseOrderInfo warehouseOrderInfo = new WarehouseOrderInfo();
                warehouseOrderInfo.setProductId(dispatchOrderInfo.getProductId());
                warehouseOrderInfo.setNum(dispatchOrderInfo.getNum());
                warehouseOrderInfoList.add(warehouseOrderInfo);
            }
            if (list.size() > 0)
            {
                dispatchOrderMapper.batchDispatchOrderInfo(list);
            }

            //更新入库产品库存（入库产品数量-出库数量）
            if (warehouseOrderInfoList.size() > 0)
            {
                dispatchOrderMapper.batchUpdateWarehouseOrderInfo(warehouseOrderInfoList);
            }
        }


    }
}
